<?php

namespace common\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\SysAdminUser;
use common\models\BaseAdminSite;

/**
 * SearchSysAdminUser represents the model behind the search form about `common\models\SysAdminUser`.
 */
class SearchSysAdminUser extends SysAdminUser
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'reg_time', 'last_login_time','site_id'], 'integer'],
            [['user_name', 'email', 'password', 'last_ip'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SysAdminUser::find();
        
        if (f_s('site_id') > 0) {
        	$suppliers = BaseAdminSite::find()->select('admin_user_id')->where(['in', 'site_id', f_s('site_list')]);
        	$query->where(['in', 'id', $suppliers]);
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'reg_time' => $this->reg_time,
            'last_login_time' => $this->last_login_time,
            'site_id'=>$this->site_id,
        ]);

        $query->andFilterWhere(['like', 'user_name', $this->user_name])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'password', $this->password])
            ->andFilterWhere(['like', 'last_ip', $this->last_ip])
            ->andFilterWhere(['like','site_id',$this->site_id]);
        return $dataProvider;
    }
}
